package com.mrzhou.web.feign;

import feign.Target;
import feign.hystrix.FallbackFactory;
import org.springframework.cglib.proxy.Enhancer;


public class CloudFallbackFactory<T> implements FallbackFactory<T> {

    private final Target<T> target;

    public CloudFallbackFactory(Target<T> target) {
        this.target = target;
    }

    @Override
    public T create(Throwable throwable) {
        final Class<T> targetType = target.type();
        final String targetName = target.name();
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(targetType);
        enhancer.setUseCache(true);
        enhancer.setCallback(new CloudFeignFallback<>(targetType, targetName, throwable));
        return (T) enhancer.create();
    }
}
